#include <iostream>
#include <string>
#include <unordered_map>

#include <yaml-cpp/yaml.h>

#include "../include/huffman_tree.hpp"

int main()
{
  YAML::Node data_file;
  try
  {
    data_file = YAML::LoadFile("../data/Data.yaml");
  }
  catch(const std::exception& e)
  {
    std::cerr << e.what() << '\n';
  }
  std::vector<char> chars = data_file["HuffmanCode"]["chars"].as<std::vector<char>>();
  std::vector<int> freq = data_file["HuffmanCode"]["freq"].as<std::vector<int>>();
  std::string test = data_file["HuffmanCode"]["data"].as<std::string>();
  std::unordered_map<char,int> m;
  for(int i=0; i<chars.size();++i)
  {
    m.insert(std::pair<char,int>(chars[i],freq[i]));
  }
  my_data::HuffmanCode huffman_code(m);
  huffman_code.input(test);
  huffman_code.initHuffmanTree();
  huffman_code.enCode();
  huffman_code.output();

  std::string code = data_file["HuffmanCode"]["code"].as<std::string>();
  std::string ans = huffman_code.deCode(code);
  std::cout<<"\n"<<code<<"的译码为：\n"<<ans<<std::endl;

  return 0;
}